package com.ruoyi.dgw.service.impl;

import java.util.List;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.dgw.domain.Insurance;
import com.ruoyi.dgw.domain.TagNumber;
import com.ruoyi.dgw.mapper.InsuranceMapper;
import com.ruoyi.dgw.service.IInsuranceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;


/**
 * 保险Service业务层处理
 *
 * @author ruoyi
 * @date 2021-11-18
 */
@Service
@Slf4j
public class InsuranceServiceImpl implements IInsuranceService
{
    @Autowired
    private InsuranceMapper insuranceMapper;

    /**
     * 查询保险
     *
     * @param id 保险主键
     * @return 保险
     */
    @Override
    public Insurance selectInsuranceById(Long id)
    {
        return insuranceMapper.selectInsuranceById(id);
    }

    @Override
    public List<Insurance> selectList2(Insurance insurance) {
        return insuranceMapper.selectList2(insurance);
    }

    /**
     * 未审核通过的树形数据
     *
     * @param insurance
     */
    @Override
    public List<Insurance> selectList1(Insurance insurance) {
        return insuranceMapper.selectList1(insurance);
    }

    /**
     * 驳回信息查询
     *
     * @param insurance
     * @return
     */
    @Override
    public List<Insurance> selectList3(Insurance insurance) {
        return insuranceMapper.selectList3(insurance);
    }

    /**
     * 查询保险列表
     *
     * @param insurance 保险
     * @return 保险
     */
    @Override
    public List<Insurance> selectInsuranceList(Insurance insurance)
    {
        return insuranceMapper.selectInsuranceList(insurance);
    }

    /**
     * 新增保险
     *
     * @param insurance 保险
     * @return 结果
     */
    @Transactional
    @Override
    public int insertInsurance(Insurance insurance)
    {
        insurance.setCreateTime(DateUtils.getNowDate());
        int rows = insuranceMapper.insertInsurance(insurance);
        insertTagNumber(insurance);
        return rows;
    }

    /**
     * 修改保险
     *
     * @param insurance 保险
     * @return 结果
     */
    @Transactional
    @Override
    public int updateInsurance(Insurance insurance)
    {
        insurance.setUpdateTime(DateUtils.getNowDate());
        insuranceMapper.deleteTagNumberByInsuranceId(insurance.getId());
        insertTagNumber(insurance);
        return insuranceMapper.updateInsurance(insurance);
    }

    /**
     * 批量删除保险
     *
     * @param ids 需要删除的保险主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteInsuranceByIds(Long[] ids)
    {
        insuranceMapper.deleteTagNumberByInsuranceIds(ids);
        return insuranceMapper.deleteInsuranceByIds(ids);
    }

    /**
     * 删除保险信息
     *
     * @param id 保险主键
     * @return 结果
     */
    @Override
    public int deleteInsuranceById(Long id)
    {
        insuranceMapper.deleteTagNumberByInsuranceId(id);
        return insuranceMapper.deleteInsuranceById(id);
    }

    @Override
    public void insertInsuranceApp(Insurance insurance) {
       List<TagNumber> tagNumberList =  insurance.getChildren();
       insuranceMapper.batchTagNumberUpdate(insurance.getInsuranceNumber(),tagNumberList);
       insurance.setInsuranceSum(Convert.toLong(tagNumberList.size()));
       log.info("单号：{}",insurance);
        insuranceMapper.insertInsuranceApp(insurance);
    }

    /**
     * 新增耳标信息
     *
     * @param insurance 保险对象
     */
    public void insertTagNumber(Insurance insurance)
    {
        List<TagNumber> tagNumberList = insurance.getChildren();
        String id = insurance.getInsuranceNumber();
        if (StringUtils.isNotNull(tagNumberList))
        {
            List<TagNumber> list = new ArrayList<TagNumber>();
            for (TagNumber tagNumber : tagNumberList)
            {
                tagNumber.setInsuranceId(id);
                list.add(tagNumber);
            }
            if (list.size() > 0)
            {
                insuranceMapper.batchTagNumber(list);
            }
        }
    }
}
